﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

using ArticleManager.DataClasses;


namespace ArticleManager.Forms
{
    public partial class frmNewArticle : Form
    {
        bool isEditMode;
        IList keywords;
        IList authors;
        Article _currentArticle;
        Controller _controller;

        public frmNewArticle()
        {
            isEditMode = false;
            _controller = Controller.Instance;
            InitializeComponent();
            PopulateKeywords();
            PopulateAuthors();
        }

        public bool EditMode
        {
            set
            {
                isEditMode = value;
            }        
        }

        public Article Article
        {
            set
            {
                _currentArticle = value;
            }
        }

        public void DisplayArticle(Article article)
        {
            _currentArticle = article;
            txtTitle.Text = article.Title;
            txtPublisher.Text = article.Publishers;
            txtReviewer.Text = article.Reviewers;
            rtfText.RichText = article.Text;
            txtTranslator.Text = article.Translators;

            foreach (ArticleKey keyword in article.KeywordList)
            {
                cboKeyWords.CheckBoxItems[_controller.GetKeyText(keyword.KeyId)].Checked = true;     
            }

            foreach (ArticleAuthor author in article.AuthorsList)
            {
                cboAuthors.CheckBoxItems[_controller.GetAuthorName(author.AuthorId)].Checked = true;
            }
        }


        private void PopulateAuthors()
        {
            authors = _controller.GetAuthors();
            foreach (Author author in authors)
            {
                cboAuthors.Items.Add(author);
            }

        }
        private void PopulateKeywords()
        {
            keywords = _controller.GetKeywords();
            foreach (Keyword keyword in keywords)
            {
                cboKeyWords.Items.Add(keyword);
            }

            authors = _controller.GetAuthors();
            foreach (Author author in authors)
            {
                cboAuthors.Items.Add(author);
            }

        }

        private bool PrepareArticle(ref Article article)
        {
            article.ArticleId = 0; /* Auto generated */
            article.Title = txtTitle.Text;
            article.Publishers = txtPublisher.Text;
            article.Reviewers = txtReviewer.Text;
            article.Text = rtfText.RichText;
            article.Translators = txtTranslator.Text;

            foreach (Keyword keyword in cboKeyWords.Items)
            {
                if (cboKeyWords.CheckBoxItems[keyword.KeyText].Checked)
                {
                    ArticleKey articleKey = new ArticleKey();
                    articleKey.ArticleId = article.ArticleId;
                    articleKey.KeyId = keyword.KeyId;
                    article.KeywordList.Add(articleKey);
                }
            }

            foreach (Author author in cboAuthors.Items)
            {
                if (cboAuthors.CheckBoxItems[author.AuthorName].Checked)
                {
                    ArticleAuthor articleAuthor = new ArticleAuthor();
                    articleAuthor.ArticleId = article.ArticleId;
                    articleAuthor.AuthorId = author.AuthorId;
                    article.AuthorsList.Add(articleAuthor);
                }
            }

            return true;
        }

        private void toolbtnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Article article = new Article();

                if (PrepareArticle(ref article))
                {
                    if (!isEditMode)
                    {
                        _controller.AddNewArticle(ref article);
                    }
                    else
                    {
                        article.ArticleId = _currentArticle.ArticleId;
                        _controller.UpdateArtcile(ref article);
                    }
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void cutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                this.rtfText.RichTextBoxCtrl.Cut();
            }
            catch
            {
                MessageBox.Show("Unable to cut document content.", "RTE - Cut", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                this.rtfText.RichTextBoxCtrl.Copy();
            }
            catch
            {
                MessageBox.Show("Unable to cut document content.", "RTE - Cut", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                this.rtfText.RichTextBoxCtrl.Paste();
            }
            catch
            {
                MessageBox.Show("Unable to cut document content.", "RTE - Cut", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
